﻿using System;
using System.Text;
using QuanLyHopDong.Biz;

namespace QuanLyHopDong.Web.HopDong
{
    public partial class VatTuTheoTuyenExport : System.Web.UI.Page
    {
        private static KhoiLuongBiz _khoiLuongBiz;

        private static KhoiLuongBiz KhoiLuongManager
        {
            get { return _khoiLuongBiz ?? (_khoiLuongBiz = new KhoiLuongBiz()); }
        }

        private int _khoiLuongId;

        protected int KhoiLuongId
        {
            get
            {
                if (_khoiLuongId == 0)
                {
                    int.TryParse(Request.Params["khoiLuongId"], out _khoiLuongId);
                }
                return _khoiLuongId;
            }
        }

        private static KhoiLuongVatTuBiz _khoiLuongVatTuBiz;
        private static KhoiLuongVatTuBiz KhoiLuongVatTuManager
        {
            get
            {
                return _khoiLuongVatTuBiz ?? (_khoiLuongVatTuBiz = new KhoiLuongVatTuBiz());
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (KhoiLuongId == 0)
            {
                return;
            }

            var html = GenerateHtml(KhoiLuongId);
            Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("content-disposition",
                string.Format("attachment;filename=Vat_Tu_Theo_Tuyen_{0:dd-MM-yyyy_hhmmss}.xls", DateTime.Now));
            Response.ContentEncoding = Encoding.UTF8;
            Response.ContentType = "application/vnd.ms-excel";
            Response.BinaryWrite(Encoding.Unicode.GetPreamble());
            var bytes = Encoding.Unicode.GetBytes(html);
            Response.BinaryWrite(bytes);
            Response.Flush();
            Response.End();
        }

        private static string GenerateHtml(int khoiLuongId)
        {
            var sbHtml = new StringBuilder();
            const string formatText = "<td style='border:solid 0.1pt;'>{0}</td>";
            const string formatTextRight = "<td style='border:solid 0.1pt;text-align:right;'>{0}</td>";

            // khoi luong
            var khoiLuong = KhoiLuongManager.GetById(khoiLuongId);
            if (khoiLuong == null)
            {
                return string.Empty;
            }

            sbHtml.Append("<html><body style='font-size:12px !important;font-family:Times New Roman !important;'><head><meta charset=\"utf-8\" />");
            sbHtml.Append("<style type='text/css'>td {vertical-align:top;}</style>");
            sbHtml.Append("</head><body>");
            sbHtml.Append("<table cellpadding='0' cellspacing='0' width='100%' style='border-collapse:collapse;'>");
            sbHtml.AppendFormat("<tr><td><strong>Mã tuyến:</strong>{0}</td></tr>", khoiLuong.MaTuyen);
            sbHtml.AppendFormat("<tr><td><strong>Điểm đầu - điểm cuối:</strong>{0} - {1}</td></tr>", khoiLuong.DiemDau, khoiLuong.DiemCuoi);
            sbHtml.AppendFormat("<tr><td><strong>Chiều dài tuyến:</strong>{0:0}</td></tr>", khoiLuong.ChieuDaiTuyen);
            sbHtml.AppendFormat("<tr><td><strong>Đội thi công:</strong>{0:0}</td></tr>", khoiLuong.TenDoiThiCong);
            sbHtml.Append("</table>");

            // vat tu
            var headers = new[]
            {
                "Số PXK", "Bên cấp", "Vật tư", "Đơn vị tính", "Số lượng", "Thi công", "Thực tế", "Ghi chú"
            };
            sbHtml.Append("<table cellpadding='0' cellspacing='0' width='100%' style='border-collapse:collapse;'>");

            // headers
            sbHtml.Append("<tr>");
            foreach (var header in headers)
            {
                sbHtml.AppendFormat("<td style='border:solid 0.1pt; text-align:center;'><strong>{0}</strong></td>", header);
            }
            sbHtml.Append("</tr>");

            var vatTus = KhoiLuongVatTuManager.KhoiLuongVatTuTheoTuyen(khoiLuongId);
            if (vatTus != null && vatTus.Count > 0)
            {
                foreach (var vatTu in vatTus)
                {
                    sbHtml.Append("<tr>");
                    sbHtml.AppendFormat(formatText, vatTu.SoPhieu);
                    sbHtml.AppendFormat(formatText, vatTu.BenCapName);
                    sbHtml.AppendFormat(formatText, vatTu.TenVatTu);
                    sbHtml.AppendFormat(formatText, vatTu.TenDonViTinh);
                    sbHtml.AppendFormat(formatTextRight, vatTu.SoLuong);
                    sbHtml.AppendFormat(formatTextRight, vatTu.ThiCong);
                    sbHtml.AppendFormat(formatTextRight, vatTu.ThucTe);
                    sbHtml.AppendFormat(formatText, vatTu.GhiChu);
                    sbHtml.Append("</tr>");
                }
            }

            sbHtml.Append("</table>");

            sbHtml.Append("</body></html>");

            return sbHtml.ToString();
        }
    }
}